package net.expectx.micro.shop.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.expectx.common.single.base.BaseResult;
import net.expectx.common.single.util.Md5Util;
import net.expectx.common.single.util.formatter.MessageFormatter;
import net.expectx.micro.shop.common.AdminConstants;
import net.expectx.micro.shop.common.AdminResult;
import net.expectx.micro.shop.common.util.ExpectXUtil;
import net.expectx.micro.shop.dao.bean.PermissionUser;
import net.expectx.micro.shop.dao.bean.PermissionUserExample;
import net.expectx.micro.shop.rpc.api.PermissionUserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author hxs
 */
@Controller
@RequestMapping("/manager/permissionUser/")
@Api(value = "用户管理", description = "用户管理")
public class PermissionUserController {
    private static final Logger LOGGER = Logger.getLogger(ManagerController.class);

    @Autowired
    PermissionUserService permissionUserService;

    @ApiOperation(value = "用户首页")
    @RequestMapping(value = "index", method = RequestMethod.GET)
    public String index(ModelMap modelMap) {
        return "permissionUser/index";
    }

    @ApiOperation(value = "新增用户")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create() {
        return "permissionUser/modify";
    }

    @ApiOperation(value = "修改用户")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") Long id, ModelMap modelMap) {
        PermissionUser permissionUser = permissionUserService.selectByPrimaryKey(id);
        modelMap.put("permissionUsers", permissionUser);
        return "permissionUser/modify";
    }

    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ResponseBody
    public Object page(
            @RequestParam(required = false, defaultValue = "1", value = "page") int page,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            HttpServletRequest request,String userName) {
        Map<String, Object> result = new HashMap<>(16);
        PermissionUserExample permissionUserExample = new PermissionUserExample();
        /**
         * 创造条件
         */
        PermissionUserExample.Criteria criteria = permissionUserExample.createCriteria();

        if (StringUtils.isNotBlank(userName)){
            criteria.andUserNameEqualTo(userName);
        }
        criteria.andIsDeletedEqualTo(AdminConstants.NO);
        /**
         * 分页
         */
        permissionUserExample.setPageIndex(page);
        permissionUserExample.setPageSize(limit);
        /**
         * 排序
         */
        permissionUserExample.setOrderByClause("gmt_modified desc");

        List<PermissionUser> permissionUsers = permissionUserService.selectByExample(permissionUserExample);

        long total = permissionUserService.countByExample(permissionUserExample);

        result.put("code", AdminConstants.LAYUI_DATA_OK);
        result.put("count", total);
        result.put("msg", "");
        result.put("data", permissionUsers);
        return result;
    }

    @ApiOperation(value = "新增用户")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(PermissionUser permissionUser) {
        permissionUser.setGmtCreate(System.currentTimeMillis());
        permissionUser.setGmtModified(System.currentTimeMillis());
        permissionUser.setIsDeleted((byte) 0);
        permissionUser.setIsLocked(AdminConstants.NO);
        permissionUser.setSalt(UUID.randomUUID().toString().toLowerCase().replace("-",""));
        permissionUser.setPassword(Md5Util.md5(permissionUser.getPassword()+permissionUser.getSalt()));
        return permissionUserService.insertSelective(permissionUser);
    }

    @ApiOperation(value = "修改用户")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable("id") Long id, PermissionUser permissionUser) {
        permissionUser.setGmtModified(System.currentTimeMillis());
        return  permissionUserService.updateByPrimaryKeySelective(permissionUser);
    }

    @ApiOperation(value = "删除用户")
    @RequestMapping(value = "/delete/{ids}", method = RequestMethod.GET)
    @ResponseBody
    public Object physicalRemove(@PathVariable("ids") String ids) {
        PermissionUserExample permissionUserExample = new PermissionUserExample();
        permissionUserExample.createCriteria().andIdIn(ExpectXUtil.toList(ids,","));
        BaseResult baseResult = permissionUserService.physicalRemoveByExample(AdminConstants.YES, permissionUserExample);
        return new AdminResult(baseResult);
    }
}
